package com.ougy.customcomponent;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ougy.customcomponent.draw.SimpleLineDrawable;
import com.ougy.customcomponent.util.MoveBg;

public class TabSwitchFrag extends RelativeLayout implements
		OnClickListener,OnFocusChangeListener {
	private static final String TAG = "TabSwitchTag";
	private int count;
	private boolean lockedA, lockedB;
	private TextView[] childViews;
	private Drawable[][] imgArr;
	private LinearLayout tab;
	private TextView frontText;
	private int singleHeight, singleWidth;
	private Drawable bottomLine;
	private int startX;
	private OnClickListener listener,mListener; 
	private OnTabSwitchListener mOnTabSwitchListener;
	private OnIndicateListener mOnIndicateListener;
	public TabSwitchFrag(Context context, AttributeSet attrs) {
		super(context, attrs);
		initViews(context);
	}

	private void initViews(Context context) {
		tab = new LinearLayout(context);
		frontText = new TextView(context);
	}

	public TabSwitchFrag(Context context) {
		super(context);
	}
	

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		if (lockedA == false) {
			customViews();
			lockedA = true;
		}
		if (getWidth() != 0 && lockedB == false) {
			singleHeight = childViews[0].getMeasuredHeight();
			customScroll();
			setOnClickListener(null);
			lockedB = true;
		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	private void customScroll() {
		singleWidth = getWidth() / count;
		drawBottomLine();
		frontText.setBackgroundColor(Color.argb(0, 0, 0, 0));
		frontText.setCompoundDrawables(null, null, null, bottomLine);
		RelativeLayout.LayoutParams paramR = new RelativeLayout.LayoutParams(
				singleWidth, singleHeight);
		paramR.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		addView(frontText, paramR);
	}

	private void drawBottomLine() {
		bottomLine = new SimpleLineDrawable(singleHeight, 0, Color.rgb(184, 0,
				0), 8);
	}

	private void customViews() {
		count = getChildCount();
		childViews = new TextView[count];
		imgArr = new Drawable[count][];
		for (int i = 0; i < count; i++) {
			try {
				childViews[i] = (TextView) getChildAt(i);
				android.widget.LinearLayout.LayoutParams paramL = new LinearLayout.LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
						1.0f);
				childViews[i].setLayoutParams(paramL);
				childViews[i].setGravity(Gravity.CENTER);
				childViews[i].setPadding(15, 15, 15, 15);
				childViews[i].setTag(i);
				childViews[i].setFocusable(true);
				childViews[i].setClickable(true);
				childViews[i].setFocusableInTouchMode(true);

			
				imgArr[i] = childViews[i].getCompoundDrawables();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		removeAllViews();
		tab.setOrientation(LinearLayout.HORIZONTAL);
		tab.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tab.setGravity(Gravity.CENTER_HORIZONTAL);
		for (int i = 0; i < count; i++) {
			tab.addView(childViews[i]);
		}
		addView(tab);
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (hasFocus == true) {
			int tag = (Integer) v.getTag();
			MoveBg.moveFrontBg(frontText, startX, singleWidth * tag, 0, 0);
			startX = singleWidth * tag;
			if(mOnTabSwitchListener!=null){
				mOnTabSwitchListener.onTabSwitch(v,tag);
			}
			if(mOnIndicateListener!=null){
				mOnIndicateListener.onIndicate(v,tag);
			}
			if(mListener!=null){
				mListener.onClick(v);
			}
			if(listener!=null){
				listener.onClick(v);
			}
		}
	}
	
	public void moveToBegin(){ 
		MoveBg.moveFrontBg(frontText, startX, 0,0,0);
		startX=0;
	}

	@Override
	public void setOnClickListener(OnClickListener listener) {
		if (listener != null) {
			this.listener = listener;
		}
		if(mListener==null){
			mListener=this;
		}
		for (int i = 0; i < count; i++) {
			childViews[i].setOnClickListener(this);
			childViews[i].setOnFocusChangeListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		if (listener != null) {
			listener.onClick(v);
		}
	}
	
	public void setOnTabSwitchListener(OnTabSwitchListener listener) {
		mOnTabSwitchListener = listener;
	}
	
	public interface OnTabSwitchListener{
		public void onTabSwitch(View v, int which);
	}
	
	public void setOnIndicateListener(OnIndicateListener listener) {
		mOnIndicateListener = listener;
	}
	
	public interface OnIndicateListener{
		public void onIndicate(View v, int which);
	}
}
